{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "     公司    股票代码  市值（亿）    市盈率\n0  杭州银行  600926    449   8.31\n1  青农商行  002958    371  15.36\n2  常熟银行  601128    237  16.01\n3  工商银行  601398  21313   7.16\n4  上海银行  601229   1369   7.59\n5  江苏银行  600919    823   6.30",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>公司</th>\n      <th>股票代码</th>\n      <th>市值（亿）</th>\n      <th>市盈率</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>杭州银行</td>\n      <td>600926</td>\n      <td>449</td>\n      <td>8.31</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>青农商行</td>\n      <td>002958</td>\n      <td>371</td>\n      <td>15.36</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>常熟银行</td>\n      <td>601128</td>\n      <td>237</td>\n      <td>16.01</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>工商银行</td>\n      <td>601398</td>\n      <td>21313</td>\n      <td>7.16</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>上海银行</td>\n      <td>601229</td>\n      <td>1369</td>\n      <td>7.59</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>江苏银行</td>\n      <td>600919</td>\n      <td>823</td>\n      <td>6.30</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 1
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "data={'公司':['杭州银行','青农商行','常熟银行','工商银行','上海银行','江苏银行'],\n",
    "'股票代码':['600926','002958','601128','601398','601229','600919'],\n",
    "'市值（亿）':[449,371,237,21313,1369,823],\n",
    "'市盈率':[8.31,15.36,16.01,7.16,7.59,6.3]}\n",
    "df_company=pd.DataFrame(data)\n",
    "df_company"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "     公司    股票代码  市值（亿）    市盈率\n0  杭州银行  600926    449   8.31\n1  青农商行  002958    371  15.36\n2  常熟银行  601128    237  16.01\n4  上海银行  601229   1369   7.59\n5  江苏银行  600919    823   6.30",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>公司</th>\n      <th>股票代码</th>\n      <th>市值（亿）</th>\n      <th>市盈率</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>杭州银行</td>\n      <td>600926</td>\n      <td>449</td>\n      <td>8.31</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>青农商行</td>\n      <td>002958</td>\n      <td>371</td>\n      <td>15.36</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>常熟银行</td>\n      <td>601128</td>\n      <td>237</td>\n      <td>16.01</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>上海银行</td>\n      <td>601229</td>\n      <td>1369</td>\n      <td>7.59</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>江苏银行</td>\n      <td>600919</td>\n      <td>823</td>\n      <td>6.30</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "df_company.loc[df_company['市值（亿）']<2000] #选出市值低于 2000 亿的所有公司"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "     公司    股票代码  市值（亿）   市盈率\n0  杭州银行  600926    449  8.31\n4  上海银行  601229   1369  7.59\n5  江苏银行  600919    823  6.30",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>公司</th>\n      <th>股票代码</th>\n      <th>市值（亿）</th>\n      <th>市盈率</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>杭州银行</td>\n      <td>600926</td>\n      <td>449</td>\n      <td>8.31</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>上海银行</td>\n      <td>601229</td>\n      <td>1369</td>\n      <td>7.59</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>江苏银行</td>\n      <td>600919</td>\n      <td>823</td>\n      <td>6.30</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "df_company.loc[df_company['市值（亿）']<2000] [df_company['市盈率']<10]\n",
    "#选出市值 < 2000亿，并且市盈率 < 10 的所有公司"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "               close \n2019-01-02   9.137135\n2019-01-03  10.248339\n2019-01-04   9.953812\n2019-01-05   9.062674\n2019-01-06   7.855551\n...               ...\n2019-04-07  11.339825\n2019-04-08  10.859544\n2019-04-09  10.769982\n2019-04-10  10.187364\n2019-04-11   9.585908\n\n[100 rows x 1 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>close</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2019-01-02</th>\n      <td>9.137135</td>\n    </tr>\n    <tr>\n      <th>2019-01-03</th>\n      <td>10.248339</td>\n    </tr>\n    <tr>\n      <th>2019-01-04</th>\n      <td>9.953812</td>\n    </tr>\n    <tr>\n      <th>2019-01-05</th>\n      <td>9.062674</td>\n    </tr>\n    <tr>\n      <th>2019-01-06</th>\n      <td>7.855551</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>2019-04-07</th>\n      <td>11.339825</td>\n    </tr>\n    <tr>\n      <th>2019-04-08</th>\n      <td>10.859544</td>\n    </tr>\n    <tr>\n      <th>2019-04-09</th>\n      <td>10.769982</td>\n    </tr>\n    <tr>\n      <th>2019-04-10</th>\n      <td>10.187364</td>\n    </tr>\n    <tr>\n      <th>2019-04-11</th>\n      <td>9.585908</td>\n    </tr>\n  </tbody>\n</table>\n<p>100 rows × 1 columns</p>\n</div>"
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "source": [
    "dr = pd.date_range(start='2019-01-02', periods=100)\n",
    "data = np.random.randn(100).cumsum()\n",
    "close = data-np.min(data)\n",
    "df = pd.DataFrame({'close ':close},index=dr)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "               close        mean\n2019-01-02   9.137135        NaN\n2019-01-03  10.248339        NaN\n2019-01-04   9.953812        NaN\n2019-01-05   9.062674        NaN\n2019-01-06   7.855551   9.251502\n...               ...        ...\n2019-04-07  11.339825  10.606095\n2019-04-08  10.859544  10.695515\n2019-04-09  10.769982  10.760213\n2019-04-10  10.187364  10.786416\n2019-04-11   9.585908  10.548524\n\n[100 rows x 2 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>close</th>\n      <th>mean</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2019-01-02</th>\n      <td>9.137135</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2019-01-03</th>\n      <td>10.248339</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2019-01-04</th>\n      <td>9.953812</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2019-01-05</th>\n      <td>9.062674</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2019-01-06</th>\n      <td>7.855551</td>\n      <td>9.251502</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>2019-04-07</th>\n      <td>11.339825</td>\n      <td>10.606095</td>\n    </tr>\n    <tr>\n      <th>2019-04-08</th>\n      <td>10.859544</td>\n      <td>10.695515</td>\n    </tr>\n    <tr>\n      <th>2019-04-09</th>\n      <td>10.769982</td>\n      <td>10.760213</td>\n    </tr>\n    <tr>\n      <th>2019-04-10</th>\n      <td>10.187364</td>\n      <td>10.786416</td>\n    </tr>\n    <tr>\n      <th>2019-04-11</th>\n      <td>9.585908</td>\n      <td>10.548524</td>\n    </tr>\n  </tbody>\n</table>\n<p>100 rows × 2 columns</p>\n</div>"
     },
     "metadata": {},
     "execution_count": 16
    }
   ],
   "source": [
    "df['mean']=df.rolling(5).mean()\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}